package cn.schoolwow.quickdao.module.database.dql.response.flow.common.resultSet;

import cn.schoolwow.quickdao.domain.entity.Entity;
import cn.schoolwow.quickdao.domain.entity.Property;
import cn.schoolwow.quickdao.module.database.dql.parent.domain.QueryContext;
import cn.schoolwow.quickdao.module.database.dql.subCondition.domain.SubQueryOption;
import cn.schoolwow.quickdao.util.StringUtil;
import cn.schoolwow.quickflow.domain.FlowContext;
import cn.schoolwow.quickflow.flow.BusinessFlow;

public class MatchQueryContextFlow implements BusinessFlow {
    @Override
    public void executeBusinessFlow(FlowContext flowContext) throws Exception {
        QueryContext queryContext = (QueryContext) flowContext.getData("queryContext");
        if(null==queryContext){
            flowContext.log("queryContext对象不存在!跳过根据queryContext对象设置");
            return;
        }
        matchQueryTable(flowContext);
        matchSubQuery(flowContext);
        flowContext.putTemporaryData("queryColumnTypeMapping", queryContext.queryOption.queryColumnOption.queryColumnTypeMapping);
        flowContext.putTemporaryData("queryColumnTypeMap", queryContext.queryOption.queryColumnOption.queryColumnTypeMap);
    }

    @Override
    public String name() {
        return "匹配查询上下文数据";
    }

    private void matchQueryTable(FlowContext flowContext){
        QueryContext queryContext = (QueryContext) flowContext.checkData("queryContext");
        Property[] resultSetPropertyArray = (Property[]) flowContext.checkData("resultSetPropertyArray");

        Entity entity = queryContext.queryOption.queryTableOption.entity;
        if(null!=entity){
            flowContext.log("根据queryContext对象的entity属性设置主表字段类型!主表:{}", entity.tableName);
            String tableAliasName = queryContext.queryOption.queryTableOption.tableAliasName;
            for(Property property:entity.properties){
                for(Property resultSetProperty:resultSetPropertyArray){
                    if(!resultSetProperty.entity.tableName.equalsIgnoreCase(entity.tableName)){
                        continue;
                    }
                    if(resultSetProperty.columnLabel.equalsIgnoreCase(tableAliasName+"_"+property.column)
                            ||resultSetProperty.column.equalsIgnoreCase(property.column)
                            || StringUtil.camel2Underline(resultSetProperty.column).equalsIgnoreCase(property.column)
                    ){
                        resultSetProperty.clazz = property.clazz;
                        break;
                    }
                }
            }
        }
    }

    private void matchSubQuery(FlowContext flowContext){
        QueryContext queryContext = (QueryContext) flowContext.checkData("queryContext");
        Property[] resultSetPropertyArray = (Property[]) flowContext.checkData("resultSetPropertyArray");

        for(SubQueryOption subQueryOption:queryContext.queryOption.subQueryOptionList){
            Entity entity = subQueryOption.subQueryTableOption.entity;
            if(null!=entity){
                flowContext.log("根据queryContext对象的entity属性设置关联表字段类型!关联表:{}", entity.tableName);
                String tableAliasName = subQueryOption.subQueryTableOption.tableAliasName;
                for(Property property:entity.properties){
                    for(Property resultSetProperty:resultSetPropertyArray){
                        if(!resultSetProperty.entity.tableName.equalsIgnoreCase(entity.tableName)){
                            continue;
                        }
                        if(resultSetProperty.columnLabel.equalsIgnoreCase(tableAliasName+"_"+property.column)
                                ||resultSetProperty.column.equalsIgnoreCase(property.column)
                        ){
                            resultSetProperty.clazz = property.clazz;
                            break;
                        }
                    }
                }
            }
        }
    }
}
